﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnalyseCrmMana;

namespace AnalyseCRM.InteractiveData
{
    public partial class MarketingListAjax : System.Web.UI.Page
    {
        private string _paramValue = "", _paramType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["paramvalue"] != null)
            {
                _paramValue = Server.UrlDecode(Request["paramvalue"].ToString());
            }
            if (Request["paramtype"] != null)
            {
                _paramType = Request["paramtype"].ToString();
            }
            Response.Write(buildHtml(_paramValue, _paramType));
        }

        public StringBuilder buildHtml(string prparamValue, string prparamType)
        {
            StringBuilder returnValue = null;
            MarketingListMana markmana = new MarketingListMana();
            MarketingFeedbackMana mfeed = null;
            switch (prparamType)
            {
                #region InsertEstimationGuest
                case "getMarketingQueryList":
                    returnValue = new StringBuilder();
                    IList<MarketingListMana> marklist = markmana.getMarketingList(prparamValue,CurSessionUser.curUser);
                    int egcount = markmana.getMarketingCount(prparamValue);
                    if (marklist != null && marklist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(marklist));
                        returnValue.Append("$" + egcount);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region SaveMarketingFound
                case "SaveMarketingFound":
                    returnValue = new StringBuilder();
                    string[] saveparam = prparamValue.Split('|');
                    int savecount = markmana.SaveMarketingFound(saveparam);
                    if (savecount > 0)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region SaveTempMarketingFound
                case "SaveTempMarketingFound":
                    returnValue = new StringBuilder();
                    int savetcount = markmana.SaveTempMarketingFound(prparamValue);
                    if (savetcount > 0)
                    {
                        markmana.builderformarketingfound(CurSessionUser.curUser);
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion

                #region exportMarketingFound
                case "exportMarketingFound":
                    returnValue = new StringBuilder();
                    IList<MarketingListMana> emarklist = markmana.GetAllMarketingList(prparamValue);
                    if (emarklist != null && emarklist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(emarklist));
                        returnValue.Append("$" + emarklist.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region importmarketingfeedback
                case "importmarketingfeedback":
                    returnValue = new StringBuilder();
                    string[] feedlist = prparamValue.Split(',');
                    int feedlistlen = feedlist.Length;
                    int rowcount = Convert.ToInt32(feedlist[feedlistlen - 1]);
                    int insertcount = 0;
                    for (int n = 0; n < feedlistlen - 1; n++ )
                    {
                        string[] feedarr = feedlist[n].Split('|');
                        mfeed = new MarketingFeedbackMana();
                        mfeed.listpcno = feedarr[0];
                        mfeed.marketingname = feedarr[1];
                        mfeed.marketingperiod = feedarr[2];
                        mfeed.guestno = feedarr[3];
                        mfeed.customerchannel = feedarr[4];
                        mfeed.salesdepartment = feedarr[5];
                        mfeed.transactiondate = feedarr[6];
                        mfeed.buyproduect = feedarr[7];
                        mfeed.transactionamount = Convert.ToDecimal(feedarr[8]);
                        if (mfeed.AddNewMarketingFeedback(mfeed) > 0)
                        {
                            insertcount ++;
                        }
                    }
                    if (rowcount == insertcount)
                    {
                        returnValue.Append("SUCCESS");
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #endregion
                case "marketingfeedback":
                    returnValue = new StringBuilder();
                    MarketingFeedbackSaleDepartment feedback = new MarketingFeedbackSaleDepartment();
                    IList<MarketingFeedbackSaleDepartment> feedbacklist = feedback.GetFeedbackSaleDepartment(prparamValue);
                    if (feedbacklist != null && feedbacklist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(feedbacklist));
                        returnValue.Append("$" + feedbacklist.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                case "GetEffectEvaluation":
                    returnValue = new StringBuilder();
                    MarketingEffectEvaluation effect = new MarketingEffectEvaluation();
                    IList<MarketingEffectEvaluation> effectlist = effect.GetEffectEvaluation(prparamValue);
                    if (effectlist != null && effectlist.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(effectlist));
                        returnValue.Append("$" + effectlist.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                case "BuilderChartF01803":
                case "BuilderChartF01804":
                case "BuilderChartF01805":
                    string[] strfound = prparamValue.Split('|');
                    returnValue = new StringBuilder();
                    string condition = " where A.CHARTNO = '" + strfound[0]
                                     + "' and A.MARKETINGTYPE = '" + strfound[1]
                                     + "' and A.USERNAME = 'admin'";
                    ChartBuildMana chartbm = new ChartBuildMana().getChartBuildBySql(condition);
                    if (chartbm != null)
                    {
                        returnValue.Append(JsonHelper.ObjectToJson(chartbm));
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                case "BuilderChartF01802":
                case "BuilderChartF01801":
                    string chartno = prparamValue;
                    returnValue = new StringBuilder();
                    ChartBuildMana chartbmm = new ChartBuildMana().getChartBuild(chartno, CurSessionUser.curUser);
                    if (chartbmm != null)
                    {
                        returnValue.Append(JsonHelper.ObjectToJson(chartbmm));
                    }
                    else
                    {
                        returnValue.Append("NONE");
                    }
                    break;
                #region 查询表1
                case "foundhistorytable1":
                    returnValue = new StringBuilder();
                    MarketingEffectEvaluation effect1 = new MarketingEffectEvaluation();
                    IList<MarketingEffectEvaluation> effectlist1 = effect1.GetHistoryEffectEvaluation(prparamValue);
                    if (effectlist1 != null && effectlist1.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(effectlist1));
                        returnValue.Append("$" + effectlist1.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                #region 查询表2
                case "foundhistorytable2":
                    returnValue = new StringBuilder();
                    MarketingFeedbackSaleDepartment feedback1 = new MarketingFeedbackSaleDepartment();
                    IList<MarketingFeedbackSaleDepartment> feedbacklist1 = feedback1.GetHistoryFeedbackSaleDepartment(prparamValue);
                    if (feedbacklist1 != null && feedbacklist1.Count > 0)
                    {
                        returnValue.Append(JsonHelper.ListToJson(feedbacklist1));
                        returnValue.Append("$" + feedbacklist1.Count);
                    }
                    else
                    {
                        returnValue.Append("none$0");
                    }
                    break;
                #endregion
                default:
                    break;
            }
            return returnValue;

        }  
    }
}